<script setup>
import { ref,reactive,onMounted,onBeforeMount } from 'vue'
import {login,getAllUsers} from '@/api/manager.js'
import { toast } from '@/composables/utils.js'
import { useRouter} from "vue-router";
import {setToken} from "@/composables/auth.js";
import {useUserInfoStore} from "@/stores/userInfo.js";

const store = useUserInfoStore()
const router = useRouter()
// do not use same name with ref
const form = reactive({
  userName: '',
  password: '',
})

const rules = reactive({
  userName: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
})

const formRef = ref(null);
const loading = ref(false);
const onSubmit = () => {
  formRef.value.validate((valid) => {
    if (!valid) {
      return false
    }else{
      loading.value = true
      login(form.userName, form.password)
          .then((res) => {
            setToken(res.token)
            toast('登录成功')


            router.push('/')
          }).finally(()=>{
            loading.value = false
      })
    }
  })
}

function handleSubmit(e) {
  if(e.key === 'Enter') {
    onSubmit()
  }
}
onMounted(()=>{
  document.addEventListener('keyup', handleSubmit)
})

onBeforeMount(() => {
  document.removeEventListener('keyup', handleSubmit)
})
</script>

<template>
  <el-row  class="login-container">
    <el-col :lg="16" :md="12" class="left">

      <div>
        <div>欢迎光临</div>
        <div>教师后端管理系统登录页面</div>
      </div>

    </el-col>
    <el-col :lg="8" :md="12" class="right">
      <h2 class="title">欢迎回来</h2>
      <div>
        <span class="line"></span>
        <span>账号密码登录</span>
        <span class="line"></span>
      </div>
      <el-form ref="formRef" :rules="rules" :model="form"  style="max-width: 600px" class="w-[250px]">
        <el-form-item prop="userName">
          <el-input  v-model="form.userName" placeholder="请输入用户名：">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="请输入密码：" type="password" show-password>
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button round type="primary" @click="onSubmit" :loading="loading" class="w-[250px]" color="#626aef">登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style scoped>
.login-container {
  @apply min-h-screen bg-indigo-500;
}

.login-container .left,.login-container .right {
  @apply flex items-center justify-center;
}
.login-container .right {
  @apply bg-light-50  flex-col;
}

.left >div>div:first-child {
  @apply font-bold text-5xl text-light-50;
}
.left >div>div:last-child {
  @apply text-gray-200 text-sm;
}

.right .title{
  @apply font-bold text-3xl text-gray-800;
}

.right >div{
  @apply flex items-center justify-center my-5 text-gray-300 space-y-2;
}

.right .line{
  @apply h-[1px] w-16 bg-gray-200;
}
</style>